package com.crm.service;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.crm.dao.Fp1DAO;
import com.crm.po.Fp1;
import com.crm.util.DateEx;
import com.crm.util.ExcelEx;
import com.crm.util.MathEx;
import com.crm.util.RequestEx;
import com.crm.util.StringEx;
import com.crm.vo.Excel;

public class Fp1Service {
	
	private static Logger logger = Logger.getLogger(Fp1Service.class);
	
	public Map<String, Object> getFp1s(HttpServletRequest request){
		StringBuffer Hql = new StringBuffer(" from Fp1 t where 1=1 ");
		int page = Integer.parseInt(request.getParameter("page"));//当前第几页
		int rows = Integer.parseInt(request.getParameter("rows"));//每页显示几行
		
		String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
		String islive = RequestEx.getStringTrim(request.getParameter("islive"));
		String supplier = RequestEx.getStringTrim(request.getParameter("supplier"));
		String rec = RequestEx.getStringTrim(request.getParameter("rec"));
		String fromDate = RequestEx.getStringTrim(request.getParameter("fromDate"));
		String toDate = RequestEx.getStringTrim(request.getParameter("toDate"));
		
		if(treeid != null && treeid.trim().length() > 0){
			Hql.append(" and t.CGroup = '");
			Hql.append(treeid);
			Hql.append("' ");
		}
		if(islive != null && islive.trim().length() > 0){
			Hql.append(" and t.islive = '");
			Hql.append(islive);
			Hql.append("' ");
		}
		if(supplier != null && supplier.trim().length() > 0){
			Hql.append(" and t.supplier like '%");
			Hql.append(supplier);
			Hql.append("%' ");
		}
		if(rec != null && rec.trim().length() > 0){
			Hql.append(" and t.rec like '%");
			Hql.append(rec);
			Hql.append("%' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() == 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '9999-12-31' ");
		}
		if(fromDate.trim().length() == 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '1970-01-01' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		
		Fp1DAO dao = new Fp1DAO();
		String countHql = "select count(t.recid) " + Hql.toString();
		int count = dao.getCount(countHql);
		Hql.append(" order by t.remitdate desc, t.creattime desc  ");
	    List<Fp1> list = dao.fp1ListPage(Hql.toString(), (page-1) * rows, rows);
	    
	    Map<String, Object> map = new HashMap<String, Object>();
	    map.put("count", count);
	    map.put("rows", list);
	    
	    return map;
	} 
	
	public int getFp1CountByTreeid(String treeid, String islive){
		StringBuffer Hql = new StringBuffer(" from Fp1 t where 1=1 ");

		if(treeid != null && treeid.trim().length() > 0){
			Hql.append(" and t.CGroup = '");
			Hql.append(treeid);
			Hql.append("' ");
		}
		if(islive != null && islive.trim().length() > 0){
			Hql.append(" and t.islive = '");
			Hql.append(islive);
			Hql.append("' ");
		}
		
		Fp1DAO dao = new Fp1DAO();
		String countHql = "select count(t.recid) " + Hql.toString();
		int count = dao.getCount(countHql);
	    
	    return count;
	} 
	
	public boolean saveOrUpdateFp1(HttpServletRequest request){
		try{
			
			String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
			
			String recid = RequestEx.getStringTrim(request.getParameter("recid"));
			Date remitdate = RequestEx.getSqlDate(request.getParameter("remitdate"));
			String supplier = RequestEx.getStringTrim(request.getParameter("supplier"));
			Double price = RequestEx.getDouble(request.getParameter("price"));
			String rec = RequestEx.getStringTrim(request.getParameter("rec"));
			String remark = RequestEx.getStringTrim(request.getParameter("remark"));
			
			Fp1DAO dao = new Fp1DAO();
			Fp1 fp1 = null;
			if(recid != null && recid.trim().length() > 0){
				fp1 = dao.findById(recid);
			}else{				
				fp1 = new Fp1();
				fp1.setCreattime(DateEx.getCurDatetimeAsLong());
				fp1.setCreater(RequestEx.getCurUser(request).getUsername());
			}
			
			fp1.setRemitdate(remitdate);
			fp1.setSupplier(supplier);
			fp1.setPrice(price);
			fp1.setRec(rec);
			fp1.setCGroup(treeid);
			fp1.setRemark(remark);
			fp1.setIslive("Y");
			
			fp1.setUpdatetime(DateEx.getCurDatetimeAsLong());
			fp1.setUpdater(RequestEx.getCurUser(request).getUsername());
			
			dao.saveOrUpdate(fp1);
		}catch(Exception e){
			logger.error("save or update fp1 fail:", e);
			return false;
		}
	    return true;
	}
	
	public boolean deleteFp1s(HttpServletRequest request){
		int result = 0;
		try{
			String recids = RequestEx.getStringTrim(request.getParameter("recids"));
			if(recids.length() > 0){
				String hql = "UPDATE Fp1 T SET T.islive = 'N' WHERE T.recid IN (" + recids + ")";
				Fp1DAO dao = new Fp1DAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("delete fp1 fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public boolean recoverFp1s(HttpServletRequest request){
		int result = 0;
		try{
			String recids = RequestEx.getStringTrim(request.getParameter("recids"));
			if(recids.length() > 0){
				String hql = "UPDATE Fp1 T SET T.islive = 'Y' WHERE T.recid IN (" + recids + ")";
				Fp1DAO dao = new Fp1DAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("recover fp1 fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public boolean reallyDeleteFp1s(HttpServletRequest request){
		int result = 0;
		try{
			String recids = RequestEx.getStringTrim(request.getParameter("recids"));
			if(recids.length() > 0){
				String hql = "DELETE FROM Fp1 T WHERE T.recid IN (" + recids + ")";
				Fp1DAO dao = new Fp1DAO();
				result = dao.executeHql(hql);
			}			
		}catch(Exception e){
			logger.error("really delete fp1 fail:", e);
			return false;
		}
	    return result > 0 ? true: false;
	}
	
	public double mathJJCC(HttpServletRequest request){
		int type = RequestEx.getInteger(request.getParameter("type"));
		Double number1 = RequestEx.getDouble(request.getParameter("n1"));
		Double number2 = RequestEx.getDouble(request.getParameter("n2"));
		
		return MathEx.mathJJCC(number1, number2, 2, type);
	}
	
	public String mathBatchAdd(HttpServletRequest request){			
		String price_temp = RequestEx.getStringTrim(request.getParameter("price_temp"));
		double gprice_sum = MathEx.mathBatchAdd(price_temp.split("_"), 2);
		
		StringBuffer buffer = new StringBuffer("");
		buffer.append("{\"price_sum\":\"" + gprice_sum + "\"}");

		return buffer.toString();
	}


	public void exportFp1s(HttpServletRequest request, HttpServletResponse response){
		StringBuffer Hql = new StringBuffer(" from Fp1 t where 1=1 ");
		
		String treeid = RequestEx.getStringTrim(request.getParameter("treeid"));
		String islive = RequestEx.getStringTrim(request.getParameter("islive"));
		String recids = RequestEx.getStringTrim(request.getParameter("recids"));
		String supplier = RequestEx.getStringTrim(request.getParameter("supplier"));
		String rec = RequestEx.getStringTrim(request.getParameter("rec"));
		String fromDate = RequestEx.getStringTrim(request.getParameter("fromDate"));
		String toDate = RequestEx.getStringTrim(request.getParameter("toDate"));
		
		if(treeid != null && treeid.trim().length() > 0){
			Hql.append(" and t.CGroup = '");
			Hql.append(treeid);
			Hql.append("' ");
		}
		if(islive != null && islive.trim().length() > 0){
			Hql.append(" and t.islive = '");
			Hql.append(islive);
			Hql.append("' ");
		}
		if(recids != null && recids.trim().length() > 0){
			Hql.append(" and t.recid in (");
			Hql.append(recids);
			Hql.append(") ");
		}
		if(supplier != null && supplier.trim().length() > 0){
			Hql.append(" and t.supplier like '%");
			Hql.append(supplier);
			Hql.append("%' ");
		}
		if(rec != null && rec.trim().length() > 0){
			Hql.append(" and t.rec like '%");
			Hql.append(rec);
			Hql.append("%' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() == 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '9999-12-31' ");
		}
		if(fromDate.trim().length() == 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '1970-01-01' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		if(fromDate.trim().length() > 0 && toDate.trim().length() > 0){
			Hql.append(" and t.remitdate BETWEEN '");
			Hql.append(fromDate);
			Hql.append("' AND '");
			Hql.append(toDate);
			Hql.append("' ");
		}
		
		Fp1DAO dao = new Fp1DAO();
		Hql.append(" order by t.remitdate desc ");
	    List<Fp1> list = dao.fp1List(Hql.toString());
	    
	    String[][] grids = new String[list.size() + 1][7];
	    String[] titles = {"汇款日期", "供应商 ", "金额 ", "发票", "备注", "创建时间", "修改时间"}; 
	    grids[0] = titles;
		for(int i = 0; i < list.size(); i ++){
			Fp1 fp1 = list.get(i);
			
			grids[i + 1][0] = StringEx.valueOf(fp1.getRemitdate(), StringEx.formatDate);
			grids[i + 1][1] = StringEx.valueOf(fp1.getSupplier());
			grids[i + 1][2] = StringEx.valueOf(fp1.getPrice());
			grids[i + 1][3] = "1".equals(fp1.getRec()) ? "已开票" : "未开票";
			grids[i + 1][4] = StringEx.valueOf(fp1.getRemark());
			grids[i + 1][5] = StringEx.valueOfLongDate(fp1.getCreattime());
			grids[i + 1][6] = StringEx.valueOfLongDate(fp1.getUpdatetime());
		}
	    ExcelEx.toExcel(request, response, new Excel("发票信息[导出时间" + DateEx.getCurDateTime() + "]", grids));
	} 
	
	public static void main(String[] args){

	}
	
}
